package com.liujg.future;

import com.google.common.collect.Lists;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.util.StringUtils;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

public class ReadCodeFromWebSyncCopy {
    public static final String baseUrl = "https://www.stats.gov.cn/sj/tjbz/tjyqhdmhcxhfdm/2023/";
    //设置utf-8发现有部分字符有乱码
    public static final String CHARSET = "UTF-8";




    /**
     * 读省的信息
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) {
        String url = baseUrl + "index.html";
        //如果需要设置代理
//        initProxy("10.10.13.200", "80");
        String htmlContent = getContent(url,0);

        Document doc = Jsoup.parse(htmlContent);
        // 选择所有省份链接
        Elements provinceLinks = doc.select("tr.PROVINCETR > td > a");
        // 创建一个虚拟线程执行器
//        ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
        List<Thread> list = Lists.newArrayList();
        for (Element link : provinceLinks) {
            /*executor.submit(()->{*/
            Thread thread = new Thread(()-> {

                String provinceName = link.text().trim();
                String href = link.attr("href").trim();

//                System.out.println("区划代码: " + code);
                System.out.println("省名称: " + provinceName);
                System.out.println("跳转地址: " + href);
                System.out.println("------------------------");


                readShi(href);

            }, link.text().trim());
            thread.start();
            list.add(thread);


            break;
        }

        for (Thread thread : list) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }

    /**
     * 读市的数据
     * @param url
     */
    public static void readShi(String url) {
        String content = getContent(baseUrl+url,0);
        Document doc = Jsoup.parse(content);
        Elements cityRows = doc.select(".citytable .citytr");

        for (Element row : cityRows) {
            Elements cells = row.select("td");

            if (cells.size() >= 2) {
                String code = cells.get(0).select("a").text();
                String cityName = cells.get(1).select("a").text();
                String cityUrl = cells.get(1).select("a").attr("href");

                System.out.println("区划代码: " + code);
                System.out.println("城市名称: " + cityName);
                System.out.println("跳转地址: " + cityUrl);
                System.out.println("------------------------");
                readXian(/*cityUrl.substring(0, cityUrl.indexOf("/")+1),*/cityUrl);
            }
        }






    }

    /**
     * 读县的数据
     * @param url
     */
    public static void readXian(String url) {
        String content = getContent(baseUrl+url,0);
        // 解析HTML
        Document doc = Jsoup.parse(content);

        // 选择表格行
        Elements countyRows = doc.select(".countytable .countytr");

        // 遍历每一行
        for (Element row : countyRows) {
            Elements cells = row.select("td");

            if (cells.size() >= 2) {
                String code = cells.get(0).text();
                String countyName = cells.get(1).select("a").text();
                String link = cells.get(1).select("a").attr("href");

                System.out.println("区划代码: " + code);
                System.out.println("区/县名称: " + countyName);
                System.out.println("跳转地址: " + link);
                System.out.println("------------------------");
                if(StringUtils.hasLength(link)){
                    readZhen(url.substring(0, url.indexOf("/")+1),link);
                }
            }
        }


    }

    /**
     * 读镇的数据
     * @param url
     */
    public static void readZhen(String prefix,String url) {
        String html = getContent(baseUrl+prefix+url,0);
        prefix = (prefix+url).substring(0, (prefix+url).lastIndexOf("/")+1);
        // 解析HTML
        Document doc = Jsoup.parse(html);

        // 选择表格行
        Elements townRows = doc.select(".towntable .towntr");

        // 遍历每一行
        for (Element row : townRows) {
            Elements cells = row.select("td");

            if (cells.size() >= 2) {
                String code = cells.get(0).text();
                String townName = cells.get(1).select("a").text();
                String link = cells.get(1).select("a").attr("href");

                System.out.println("区划代码: " + code);
                System.out.println("镇名称: " + townName);
                System.out.println("跳转地址: " + link);
//                System.out.println("跳转地址prefix: " + prefix);
                System.out.println("------------------------");
                readCun(prefix,link);
            }
        }


    }

    /**
     * 读村/街道的数据
     * @param url
     */
    public static void readCun(String prefix,String url) {
        String content = getContent(baseUrl+prefix+url,0);
        // 解析HTML
        Document doc = Jsoup.parse(content);

        // 选择表格行
        Elements villageRows = doc.select(".villagetable .villagetr");

        // 遍历每一行
        for (Element row : villageRows) {
            Elements cells = row.select("td");

            if (cells.size() >= 3) {
                String code = cells.get(0).text();
                String villageName = cells.get(2).text();

                System.out.println("区划代码: " + code);
                System.out.println("村名称: " + villageName);
                System.out.println("------------------------");
            }
        }

    }


    //设置代理
    public static void initProxy(String host, String port) {
        System.setProperty("http.proxyType", "4");
        System.setProperty("http.proxyPort", port);
        System.setProperty("http.proxyHost", host);
        System.setProperty("http.proxySet", "true");
    }

    public static void readHttps() throws NoSuchAlgorithmException, KeyManagementException {
        // 创建一个信任所有证书的 TrustManager
        TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        }};

        // 初始化 SSLContext 使用信任所有证书的 TrustManager
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());

        // 设置 HostnameVerifier 为信任所有主机名
        HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);
    }

    //获取网页的内容
    public static String getContent(String strUrl,int count) {

        try {
            readHttps();
            URL url = new URI(strUrl).toURL();
            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), Charset.forName(CHARSET)));
            String s;
            StringBuilder sb = new StringBuilder();
            while ((s = br.readLine()) != null) {
                sb.append(s);
            }


            br.close();
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }
}
